package com.hxzy.shop.utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;

/**
 * 创建人 ： wq
 * 创建时间 ： 2022/4/26 9:25
 * 功能描述 ： 营销通签名
 */
public class YxtSigUitls {

    // 编码方式
    private static final String CONTENT_CHARSET = "UTF-8";
    // HMAC算法
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

    public static void main(String[] args) {
        HashMap<String, String> params = new HashMap<>();
        params.put("TenantName", "jiabai");
        params.put("TenantLoginUrl", "http://36q6332119.goho.co/headquarters/yxt/login?TenantName=jiabai");
        params.put("TenantRedirectUrl", "http://36q6332119.goho.co/headquarters/yxt/callback");
        params.put("ClientId", "ai-5a1b68494e0a47fab3fc1bc87c8f19de");
        params.put("ClientSecret", "skcrgACXcE1ftjRB1mhq8shy");
        params.put("AuthorizationEndpoint", "https://jiabai-idp.cig.tencentcs.com/sso/tn-150aa6871ef24b4db8f911092fe3c6e0/ai-5a1b68494e0a47fab3fc1bc87c8f19de/oidc/authorize");
        params.put("TokenEndpoint", "https://jiabai-idp.cig.tencentcs.com/sso/tn-150aa6871ef24b4db8f911092fe3c6e0/ai-5a1b68494e0a47fab3fc1bc87c8f19de/oidc/token");
        params.put("UserInfoEndpoint", "https://jiabai-idp.cig.tencentcs.com/sso/tn-150aa6871ef24b4db8f911092fe3c6e0/ai-5a1b68494e0a47fab3fc1bc87c8f19de/oidc/userinfo");
        params.put("WellKnownEndpoint", "https://jiabai-idp.cig.tencentcs.com/sso/tn-150aa6871ef24b4db8f911092fe3c6e0/ai-5a1b68494e0a47fab3fc1bc87c8f19de/oidc/.well-known/openid-configuration");
        params.put("RequestSource", "qianfanym");
        params.put("TimeStamp", "1650953331");
        try {
            String paramsLink = makeSource(params);
            String sig = makeSig(paramsLink);
            System.out.println(sig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 获取原串
    public static String makeSource(HashMap<String, String> params) throws Exception {
        Object[] keys = params.keySet().toArray();

        Arrays.sort(keys);
        StringBuilder buffer = new StringBuilder();
        int length = keys.length;
        for (int i = 0; i < length; i++) {
            buffer.append(keys[i]).append("=").append(params.get(keys[i]).trim());
            if (i != length - 1) {
                buffer.append("&");
            }
        }
        try {
            String replace = URLEncoder.encode(buffer.toString(), CONTENT_CHARSET).replace("*", "%2A");
            return replace;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    // 生成加密字符串
    public static String makeSig(String sourceStr) throws Exception {
        try {
            String yztKey = "BBHa54quEuW9yUkqguvCnDWCpOmvgXZO" + "&";
            SecretKeySpec signingKey = new SecretKeySpec(yztKey.getBytes(), HMAC_SHA1_ALGORITHM);
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(signingKey);
            byte[] digest = mac.doFinal(sourceStr.getBytes());
            return URLEncoder.encode(Base64.getEncoder().encodeToString(digest), CONTENT_CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

}
